# app/__init__.py

import os
from flask import Flask
from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()

def create_app():
    app = Flask(__name__)

    # ========== 数据库配置 ==========
    # 永远使用 PyMySQL 驱动，避免 mysql-connector-python 在 Python<3.7 中的 future annotations 错误
    app.config['SQLALCHEMY_DATABASE_URI'] = (
        'mysql+pymysql://root:root@localhost:3306/chiibug?charset=utf8mb4'
    )
    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
    # 如果此时又出现 “cryptography package is required” 错误，可在 MySQL 里执行：
    #   ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root';
    # 或者在虚拟环境里安装 cryptography：pip install cryptography
    # ================================


    # 管理员密码（Excel 上传和微信管理口令校验）
    app.config['ADMIN_PASSWORD'] = '123456'
    # 微信验证用的 Token
    app.config['WECHAT_TOKEN'] = "wyt0206"

    # 文件上传目录
    app.config['UPLOAD_FOLDER'] = 'temp'
    if not os.path.exists(app.config['UPLOAD_FOLDER']):
        os.makedirs(app.config['UPLOAD_FOLDER'])

    # 初始化扩展
    db.init_app(app)

    # 注册蓝图
    from app.api.wechat import wechat_bp
    from app.api.salary import salary_bp

    app.register_blueprint(wechat_bp)
    app.register_blueprint(salary_bp, url_prefix='/salary')

    return app

__all__ = ['create_app', 'db']